import {
	storeBindingsBehavior
} from 'mobx-miniprogram-bindings'
import {
	store
} from '../../store/store'
import wrapRequest from '../../utils/request'
const shareUtils = require('../../utils/share')
Page({
	data: {
		products: [],
		notices: ["本期特价菜：凉瓜焖鱼", "本期特价菜：铁板鱿鱼", "本期特价菜：韭黄炒蛋", "本期特价菜：蒜香骨"],
		currentNoticeIndex: -1,
	},
	behaviors: [storeBindingsBehavior],
	storeBindings: {
		store,
		actions: {
			updateActive: 'updateActiveTabBarIndex'
		}
	},
	onLoad() {
		var that = this
		wrapRequest('/v1.0/huichun/home/notice', 'GET', {
			success: function (res) {
				let notices = []
				let products = []
				res.data.data?.map(item => {
					notices.push(item.name)
					products.push(item)
				})
				that.setData({
					notices: notices,
					products: products,
				})
				if (products.length > 0) {
					that.setData({
						currentNoticeIndex: 0,
					})
				}
			}
		})
	},
	toShop() {
		this.updateActive(1)
		wx.switchTab({
			url: '/pages/market/list/list',
		})
	},
	noticeClick(e) {
		var productId = 0
		if (this.data.products[this.data.currentNoticeIndex]?.product_id) {
			productId = this.data.products[this.data.currentNoticeIndex].product_id
		}
		console.log("click:", productId)
		if (productId > 0) {
			wx.navigateTo({
				url: '/pages/market/detail/detail?id=' + productId
			})
		}
	},
	noticeChange(e) {
		this.setData({
			currentNoticeIndex: e.detail.current,
		})
	},
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {
		return shareUtils.getShareConfig();
	},
	onShareTimeline() {
		return shareUtils.getShareConfig();
	}
})